/**
 * 应用程序入口
 * 创建时间：2025-09-11
 * 作者：开发团队
 * 目的：初始化Vue应用和配置各个插件
 */

import './assets/main.css'
import 'element-plus/dist/index.css'
import 'element-plus/theme-chalk/dark/css-vars.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import ElementPlus from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

import App from './App.vue'
import router from './router'
import { useAuthStore } from './stores/auth'
import { useThemeStore } from './stores/theme'

const app = createApp(App)

// 创建Pinia实例并添加持久化插件
const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)

// 注册Pinia状态管理
app.use(pinia)

// 注册路由
app.use(router)

// 注册Element Plus
app.use(ElementPlus)

// 注册Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// 初始化应用
const initApp = async (): Promise<void> => {
  // 初始化主题
  const themeStore = useThemeStore()
  themeStore.initTheme()
  
  // 初始化认证状态
  const authStore = useAuthStore()
  await authStore.initAuth()
}

// 启动应用
initApp().then(() => {
  app.mount('#app')
}).catch((error) => {
  console.error('应用初始化失败:', error)
  app.mount('#app')
})